package com.hax2max.control.externalclient;

import android.util.Log;

import com.hax2max.R;
import com.hax2max.control.FriendStalkerException;
import com.hax2max.control.externalclient.facebookAPI.FacebookConnector;
import com.hax2max.control.externalclient.facebookAPI.FacebookConnector.FbStatus;
import com.hax2max.control.externalclient.facebookAPI.FacebookConnectorException;
import com.hax2max.model.ExternalData;
/**
 * Used for friend stalker facebook communication
 * @author Peter
 *
 */
public class FacebookClient implements IExternalClient {

	FacebookConnector facebookHandle;
	
	
	
	/**
	 * 
	 * @param email
	 * user email (used as login name)
	 * @param password
	 * user password
	 * @throws FriendStalkerException.IExternalClient
	 */
	public FacebookClient(String email, String password) throws FriendStalkerException.IExternalClient
	{
		facebookHandle = new FacebookConnector();
		try{
		facebookHandle.login(email, password);
		}
		catch (FacebookConnectorException ex)
		{
			Log.e("FacbookClient", ex.getMessage());
			switch (ex.errorID){
			case FacebookConnectorException.HttpConnectionError:
				throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientInvalidConnectionError);
			case FacebookConnectorException.InvalidUsernamePassword:
				throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientNoUserPass);
			case FacebookConnectorException.MissingApplicationPermission:
			case FacebookConnectorException.MissingExtendedPermission:
			default:
					throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientInvalidConnectionError);
			}
		}
	}
	
	public boolean testUserValid() {
		return true;
	}

	/**
	 * @param ed
	 * External Data object containing desired status to be published on facebook
	 * @return
	 * true upon success
	 * false if not succeeded
	 */
	public boolean updateStatus(ExternalData ed) {
		try{
		return facebookHandle.setStatus(ed.statusText);
		}
		catch(FacebookConnectorException e)
		{
			Log.e("FacebookClient", e.getMessage());
			return false;
		}
	}

	/**
	 * @param ed
	 * External Data object containing the facebook id of a user
	 * @returns
	 * External Data object containing the users latest status update
	 */
	public ExternalData loadData(ExternalData ed) {
		FbStatus status;
		try{
		status = facebookHandle.getStatus(ed.userId);
		ed.statusText =  status.message;
		ed.latestUpdate = status.timestamp;
		return ed;
		}
		catch(FacebookConnectorException e)
		{
			Log.e("FacebookClient", "Error Message:"+ e.getMessage());
			ed.success = false;
			ed.errorMsg = e.getMessage();
			return ed;
		}
		
	}
	
public class OtherMethods{
	
		public final static String ext_perm_method = "ext_perm";
		public final static String ext_perm_param_email = "email";
		public final static String ext_perm_param_read_stream ="read_stream";
		public final static String ext_perm_param_publish_stream ="publish_stream";
		public final static String ext_perm_param_offline_access ="offline_access";
		public final static String ext_perm_param_status_update="status_update";
		public final static String ext_perm_param_photo_upload ="photo_upload";
		public final static String ext_perm_param_create_event ="create_event";
		public final static String ext_perm_param_rsvp_event ="rsvp_event";
		public final static String ext_perm_param_sms ="sms";
		public final static String ext_perm_param_video_upload ="video_upload";
		public final static String ext_perm_param_create_note ="create_note";
		public final static String ext_perm_param_share_item ="share_item";
		
		public final static String getApiKey_method = "getApiKey";
		public final static String getApiKey_param = "";

		
		
	}
	
	/**
	 * @param method 
	 * ext_perm accepts one parameter (the extended facebook permission) returns boolean true if exists false if not 
	 */
	public Object callOtherMethod(String method, String... parameter)
			throws FriendStalkerException.IExternalClient {

		if (method.equals(OtherMethods.ext_perm_method)) 
		{
			try {
			    for (int i=0;i<parameter.length;i++)
				if (!facebookHandle.hasAppPermission(parameter[i])) return false;
			    return true;
			} catch (FacebookConnectorException e) {
				Log.e("FacebookClient", e.getMessage());
				throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientMethodError); 
				}
		}
		else if(method.equals(OtherMethods.getApiKey_method))
			return facebookHandle.getApiKey();
		
		throw new FriendStalkerException.IExternalClient(R.string.Exception_FriendStalker_IExternalClientMethodNotImplemented);
	}

	public IExternalClient changeUserAndLogin(String userId, String password)
			throws FriendStalkerException.IExternalClient {

		return new FacebookClient(userId, password);
	}

}
